package com.gljx.web.controller.JournalController;

import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.JournalEntry.StudentStateJournal;
import com.gljx.web.Entity.User;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.JournalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/")
public class StudentStateJournalController {

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private JournalService journalService;

    /**
     * @Author lcx
     * @Date 2017/12/18 0018 上午 9:42
     * 查询加油站进销存统计报表
     */
    @RequestMapping(value = "/chooseStudentStateDetailJournal/look.action")
    public String chooseStudentStateDetailJournal(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");                                   //获取已经登录用户数据
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
        Calendar cale = Calendar.getInstance();// 得到一个Calendar的实例
        cale.add(Calendar.MONTH, -1);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        Calendar cale2 = Calendar.getInstance();// 得到一个Calendar的实例
        cale2.add(Calendar.MONTH, 0);
        cale2.set(Calendar.DAY_OF_MONTH, 0);
        Date resultDate = cale.getTime(); // 结果
        Date resultDate2 = cale2.getTime(); // 结果
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String beginTime = sdf.format(resultDate);
        String endTime = sdf.format(resultDate2);

        request.setAttribute("company", department);
        request.setAttribute("beginTime", beginTime);
        request.setAttribute("endTime", endTime);

        /*request.setAttribute("beginTime", "2017-01-01");
        request.setAttribute("endTime", "2017-12-30");*/

        return "backHome/journaling/manageJournaling/StudentStateJournaling/StudentStateJournalingList";

    }

    /**
     * @Author lcx
     * @Date 2017/12/21 0021 下午 2:37
     * 查询学生状态表
     */
    @RequestMapping(value = "/selStudentStateJournal")
    @ResponseBody
    @Transactional
    public Map selStudentStateDetailJournal(HttpServletRequest request, @RequestParam(defaultValue = "", value = "cfoilspecName") String cfoilspecName,
                                            @RequestParam(defaultValue = "", value = "begindate") String begindate,
                                            @RequestParam(defaultValue = "", value = "enddate") String enddate,
                                            @RequestParam(defaultValue = "", value = "state") String state) {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");                                   //获取已经登录用户数据
        Department department = departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
        //1.查询
        Map returnmap = new HashMap();

        Map map = new HashMap();
        map.put("companyId", department.getFid());
        map.put("beginTime", begindate);
        map.put("endTime", enddate);
        map.put("table", state);

        List<StudentStateJournal> studentStateJournals = journalService.selectStudentState(map);

        returnmap.put("state", state);
        returnmap.put("carExpense", studentStateJournals);
        returnmap.put("gsName", department.getFnameL2());

        //查询期末结存

        return returnmap;
    }

}
